/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.ejb.sessionbean;

import cassys.dataaccess.DAOException;
import cassys.dataaccess.DAOFactory;
import cassys.dataaccess.UserDAO;
import cassys.datatransferobjects.LecturerDTO;
import cassys.datatransferobjects.StudentDTO;
import cassys.datatransferobjects.UserDTO;
import cassys.util.CassysEjbConstants;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

/**
 *
 * @author ASUS
 */
@Stateless(mappedName = "UserBean")
public class UserBean implements UserBeanRemote {

    private UserDAO userDAO = null;

    public UserBean() {
    }

    public void ejbCreate() {

        userDAO = DAOFactory.getInstance().getUserDAO();

    }

    @Override
    public ArrayList<UserDTO> findAllUsers() throws DAOException {
        ArrayList<UserDTO> userList = null;


        try {
            userList = userDAO.findAllUser();

        } catch (DAOException e) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", e);

        }
        return userList;
    }

    @Override
    public UserDTO findUser(String userId) throws DAOException {
        UserDTO usr = null;
        usr = userDAO.findUser(userId);
        return usr;
    }
    
    @Override
    public UserDTO findUserByName(String username) throws DAOException {
        UserDTO usr = null;
        usr = userDAO.findUserByName(username);
        return usr;
    }    

    @Override
    public void updateUser(UserDTO user) throws DAOException {
        Logger.getLogger(getClass().getName()).log(Level.INFO, " updating user");
        userDAO.insertOrUpdateUser(user);
    }

    @Override
    public void insertUser(UserDTO user) throws DAOException {
       Logger.getLogger(getClass().getName()).log(Level.INFO, " inserting user");
            
        user.setUserId("");
        
        if (user.getRole().equals("lecturer")) {
            LecturerDTO lecturer = new LecturerDTO();
            lecturer.setUsername(user.getUsername());
            lecturer.setLecturername(user.getUsername());
            lecturer.setPassword(user.getPassword());
            lecturer.setEmail(user.getEmail());
            lecturer.setRole(user.getRole());
            lecturer.setUserId("L" + getRandomNumber());
            if(findUser(lecturer.getUserId()) != null){
                insertUser(user);
            }else{
                userDAO.insertOrUpdateUser(lecturer);
            }
        } else if (user.getRole().equals("student")) {
            StudentDTO student = new StudentDTO();
            student.setUsername(user.getUsername());
            student.setStudentName(user.getUsername());
            student.setPassword(user.getPassword());
            student.setEmail(user.getEmail());
            student.setRole(user.getRole());
            student.setUserId("S" + getRandomNumber());
            if(findUser(student.getUserId()) != null){
                insertUser(user);
            }else{
                userDAO.insertOrUpdateUser(student);
            }
        } else {
            user.setUserId("A" + getRandomNumber());
            if(findUser(user.getUserId()) != null){
                insertUser(user);
            }else{
                userDAO.insertOrUpdateUser(user);
            }
        }
    }

    @Override
    public void deleteUser(UserDTO user) throws DAOException {
        userDAO.deleteUser(user);
    }

    @Override
    public boolean userValidate(String username, String password) throws DAOException {
        try {
            UserDTO usr = userDAO.findUserByName(username);
            //UserDTO usr = findUser(username);
            if (null != usr && null != username && null != password) {
                Logger.getLogger(getClass().getName()).log(Level.INFO, "User Name " + username + ", " + usr.getUsername());
                Logger.getLogger(getClass().getName()).log(Level.INFO, "User Password " + password + ", " + usr.getPassword());
                if (username.trim().equalsIgnoreCase(usr.getUsername()) && password.trim().equalsIgnoreCase(usr.getPassword())) {
                    return true;
                }
            }
        } catch (Exception e) {
            System.err.println("DOA Exception in Validate and Find User" + e);
            throw new DAOException(e);
        }
        return false;
    }

    @Override
    public boolean isRoleAdmin(String userid) throws DAOException {
        try {
            UserDTO usr = findUser(userid);
            Logger.getLogger(getClass().getName()).log(Level.INFO, " is Role Admin" + usr.getRole());
            if (usr.getRole().trim().equalsIgnoreCase(CassysEjbConstants.ADMIN)) {
                return true;
            }
        } catch (Exception e) {
            System.err.println("DAO Exception in isRoleAdmin" + e);
            throw new DAOException(e);
        }
        return false;
    }

    protected int getRandomNumber() {
        Random randomGenerator = new Random();
        return randomGenerator.nextInt();
    }
}